<?php

namespace Common\Model;

/**
 * HotelCouponModel.class.php实例模型
 * 
 * @author wenhui
 * @date 2017-011-09
 */
class HotelCouponModel extends BaseModel {

    protected $trueTableName = 'bk_hotel_coupon';

    /**
     * 添加数据
     * @modify wenhui 2017-01-02    创建
     * @param  int   $hotelId    客栈ID
     * @param  string   $name    名称
     * @param  int   $couponType   优惠券类型 1=满减券 2=折扣券
     * @param  int   $price    满减金额
     * @param  int   $discount    折扣值
     * @param  int   $number    优惠券数量
     * @param  string   $startDate    开始时间
     * @param  string   $endDate    结束时间
     * @param  string   $week    禁用星期 '1,2,3,4,5,6,0'
     * @param  int   $useMan    使用人群 1=全部 2=会员 3=非会员
     * @param  decimal   $minusPrice    立减金额
     * @param  int   $useNumber    每个账号对该优惠券最多可领取次数
     */
    public function addCoupon($hotelId, $name, $couponType, $price, $discount, $number, $startDate, $endDate, $week, $useMan, $minusPrice, $useNumber) {
        $data = array(
            'hotelentity_id' => $hotelId,
            'name' => $name,
            'type' => $couponType,
            'price' => $price,
            'discount' => $discount,
            'number' => $number,
            'startdate' => $startDate,
            'enddate' => $endDate,
            'week' => $week,
            'crowd' => $useMan,
            'minus_price' => $minusPrice,
            'receive_number' => $useNumber,
        );

        return $this->add($data);
    }

    /**
     * 获取数据
     * @modify wenhui 2017-01-02    创建
     * @param  int   $hotelId    客栈ID
     * @return data
     */
    public function getCoupon($hotelId) {
        $option = [
            'hotelentity_id'=> $hotelId,
            'isdelete'=> NOT_DELETE,
        ];

        $field = 'id,hotelentity_id,name,no,type,price,discount,number,startdate,enddate,week,crowd,minus_price,receive_number,roomtype,createtime';
        $data = $this->where($option)->field($field)->select();

        return empty($data) ? array() : $data;
    }

    /**
     * 优惠券码
     * @modify wenhui 2018-01-02
     * @param string $no  优惠券码
     * @return type
     */
    public function saveCouponNo($couponId, $no) {
        $data = array();

        $options['id'] = $couponId;
        $data['no'] = $no;

        return $this->where($options)->save($data) === false ? false : true;
    }

    /**
     * 修改数据
     * @modify wenhui 2017-01-02    创建
     * @param  int   $couponId    ID
     * @param  string   $name    名称
     * @param  string   $startDate    开始时间
     * @param  string   $endDate    结束时间
     * @param  string   $week    禁用星期 '1,2,3,4,5,6,0'
     * @param  int   $useMan    使用人群 1=全部 2=会员 3=非会员
     * @param  int   $useNumber    每个账号对该优惠券最多可领取次数
     * @param  int   $roomType    房间类型 1=间夜房 2=钟点房 3=长租房
     */
    public function saveCoupon($couponId, $name, $startDate, $endDate, $week, $useMan, $useNumber, $roomType) {
        $data = array();

        $options['id'] = $couponId;

        $data['name'] = $name;
        $data['startdate'] = $startDate;
        $data['enddate'] = $endDate;
        $data['week'] = $week;
        $data['crowd'] = $useMan;
        $data['receive_number'] = $useNumber;
        $data['roomtype'] = $roomType;

        return $this->where($options)->save($data) === false ? false : true;
    }

    /**
     * 删除优惠券
     * @modify wenhui 2018-01-02
     * @param  int   $couponId    ID
     * @return type
     */
    public function deleteCoupon($couponId) {
        $data = array();

        $options['id'] = $couponId;
        $data['isdelete'] = IS_DELETE;

        return $this->where($options)->save($data) === false ? false : true;
    }

    /**
     * 获取数据
     * @modify wenhui 2017-01-02    创建
     * @param  int   $ids
     * @param  bool   $isDelete 是否获取删除记录
     * @return data
     */
    public function getCouponByIds($ids, $isDelete = false) {
        if (is_array($ids)) {
            $ids = implode(',', $ids);
        }

        if (strpos($ids, ',') !== false) {
            $option['id'] = array('in', $ids);
        } else {
            $option['id'] = $ids;
        }

        if (!$isDelete) {
            $option['isdelete'] = NOT_DELETE;
        }

        $field = 'id,hotelentity_id,name,no,type,price,discount,number,startdate,enddate,week,crowd,minus_price,receive_number,roomtype,createtime';
        $data = $this->where($option)->field($field)->select();

        return empty($data) ? array() : $data;
    }

    /**
     * 获取数据
     * @modify wenhui 2017-01-02    创建
     * @param  int   $ids
     * @param  bool   $isDelete 是否获取删除记录
     * @return data
     */
    public function getInfoById($id) {
        $option['id'] = $id;
        $currentDate =  date('Y-m-d') . ' 00:00:00';
        $option['enddate'] = array('egt' ,$currentDate);
        $option['isdelete'] = NOT_DELETE;

        $field = 'id,hotelentity_id,name,no,type,price,discount,number,startdate,enddate,week,crowd,minus_price,receive_number,roomtype,createtime';
        $data = $this->where($option)->field($field)->find();

        return empty($data) ? array() : $data;
    }
}
